package dataBase;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class FillDBTables {

	private static final String INSERT_INTO_BUILDINGS_TABLE = "INSERT INTO buildings(barack_id, goldmine_id, foodstore_id) "
			+ "VALUES(1, 2, 3) ";
	private static final String INSERT_INTO_USERS_TABLE = "INSERT INTO users(user_id, username, "
			+ "password, age, race_id, city_id, gold, food, army_capacity, army, food_production, gold_production) "
			+ "VALUES(user_seq.NEXTVAL, ?, ?, ?, ?, ?, 5000, 5000, 50, 0, 200, 200) ";
	private static final String INSERT_INTO_HP_AD_LEVELS_TABLE = "INSERT INTO hp_ad_levels(username, barbarianHPLevel, "
			+ "barbarianADLevel, archerHPLEvel, archers, barbarians, "
			+ "wizzards, archerADLevel, wizzardADLevel, wizzardHPLevel) "
			+ "VALUES(?, 1, 1, 1, 0, 0, 0, 1, 1, 1)";
	// Some of these are not private because I'm using them in other classes as
	// well.
	static final int CITY_CONSTANT = 101;
	static final String WARRIOR_RACE = "Warrior";
	static final String ELF_RACE = "Elf";
	static final String HUMAN_RACE = "Human";
	private static final String INSERT_INTO_USER_CITY_TABLE = "INSERT INTO user_city(city_id, city_name) VALUES(";
	private static final String INSERT_INTO_USER_RACE_TABLE = "INSERT INTO user_race(race_id, race_name) VALUES(";
	private static PreparedStatement statement;

	static void fillUserCityTable(Connection dbCon) throws SQLException {
		String[] cities = citiesArray();
		for (int i = ForeignKeyCheck.INITIALIZATION; i < cities.length; i++) {
			statement = dbCon.prepareStatement(INSERT_INTO_USER_CITY_TABLE
					+ (i + CITY_CONSTANT) + ", ?) ");
			statement.setString(1, cities[i]);
			statement.executeUpdate();
		}
	}

	static void fillUserRaceTable(Connection dbCon) throws SQLException {
		for (int i = ForeignKeyCheck.HUMAN_RACE_ID; i <= ForeignKeyCheck.WARRIOR_RACE_ID; i += ForeignKeyCheck.HUMAN_RACE_ID) {
			statement = dbCon.prepareStatement(INSERT_INTO_USER_RACE_TABLE + i
					+ ", ?) ");
			if (i == ForeignKeyCheck.HUMAN_RACE_ID)
				statement.setString(1, HUMAN_RACE);
			else if (i == ForeignKeyCheck.ELF_RACE_ID)
				statement.setString(1, ELF_RACE);
			else
				statement.setString(1, WARRIOR_RACE);
			statement.executeUpdate();
		}
	}

	static void insertIntohp_ad_levelsTable(Connection dbCon, String username)
			throws SQLException {
		statement = dbCon.prepareStatement(INSERT_INTO_HP_AD_LEVELS_TABLE);
		statement.setString(1, username);
		statement.executeUpdate();
	}

	static void insertIntoUsersTable(Connection dbCon, String username,
			String password, int age, int raceNumber, int cityNumber, int gold,
			int food, int army_capacity, int army) throws SQLException {
		statement = dbCon.prepareStatement(INSERT_INTO_USERS_TABLE);
		statement.setString(1, username);
		statement.setString(2, password);
		statement.setInt(3, age);
		statement.setInt(4, raceNumber);
		statement.setInt(5, cityNumber);
		statement.executeUpdate();
	}

	static void insertIntoBuildingsTable(Connection dbCon) throws SQLException {
		statement = dbCon.prepareStatement(INSERT_INTO_BUILDINGS_TABLE);
		statement.executeUpdate();
	}

	static String[] citiesArray() {
		String[] cities = { "Sofia", "Aitos", "Burgas", "Gabrovo", "Haskovo",
				"Montana", "Pazardjk", "Plovdiv", "Ruse", "Smolian", "Varna",
				"V. Turnovo", "Yambol" };
		return cities;
	}
}
